//
// Created by jiaxv on 25-9-9.
//
#include "services/input.h"
#include <ld_log.h>

#include "as_entity.h"
#include "elements.h"


static void gsg_input_service(int argc, char **argv);
static void amm_input_service(int argc, char **argv);
static void dbsm_input_service(int argc, char **argv);
static void rdf_input_service(int argc, char **argv);

static const size_t funcs_sz = 4;
static terminal_func terminal_funcs[] = {
    gsg_input_service,
    amm_input_service,
    dbsm_input_service,
    rdf_input_service,
};

void *listening_input(void *args) {
    char input[1024];
    while (TRUE) {
        printf("> "); // 提示符
        fflush(stdout); // 确保提示符立即显示

        if (fgets(input, sizeof(input), stdin) == NULL || !strncmp(input, "exit", 4)) {
            kill(-getpid(), SIGINT);
            break;
        }

        // 去除末尾的换行符
        input[strcspn(input, "\n")] = '\0';

        if (strlen(input)) {
            char **argv;
            int argc = ld_split(input, &argv);
            if (argc == 0 || argv == NULL) { continue; }
            long serial = strtol(argv[0], NULL, 10);
            if (serial >= funcs_sz) {
                log_warn("Invalid func serial number");
                continue;
            }
            terminal_funcs[serial](argc, argv);
            free(argv);
        }

        // 处理输入过长导致缓冲区未读完的情况
        if (strlen(input) == sizeof(input) - 1 && input[sizeof(input) - 2] != '\n') {
            int ch;
            while ((ch = getchar()) != '\n' && ch != EOF); // 清空缓冲区残留
        }
    }
    return NULL;
}

void start_input_service(input_service_t *service) {
    pthread_create(&service->th, NULL, listening_input, NULL);
    pthread_detach(service->th);
}

static void amm_input_service(int argc, char **argv) {
    net_element_t *ele = get_element_by_tag(ELEMENT_AMM, "AMM");
    if (!ele) {
        log_warn("No net element");
        return;
    }

    print_as_info(ele, ele->as_map, SERIAL_TAG | SAC_TAG | UA_TAG | CURRENT_PEER_TAG | STATUS_TAG);
}

static void dbsm_input_service(int argc, char **argv) {
    net_element_t *ele = get_element_by_tag(ELEMENT_DBSM, "DBSM");
    if (!ele) {
        log_warn("No net element");
        return;
    }

    print_as_info(ele, ele->as_map,  SERIAL_TAG |SAC_TAG | AS_IP_TAG | GS_IP_TAG | GS_PORT_TAG | STATUS_TAG);
}

static void gsg_input_service(int argc, char **argv) {
    if (argc != 2) {
        log_warn("Wrong para for gsg infos");
        return;
    }

    net_element_t *ele = get_element_by_tag(ELEMENT_GSG, strcmp("16", argv[1]) == 0 ? "GSG_010" : "GSG_011");
    if (!ele) {
        log_warn("No net element");
        return;
    }

    print_as_info(ele, ele->as_map,  SERIAL_TAG | SAC_TAG | UA_TAG | AS_IP_TAG | STATUS_TAG);
}



static void rdf_input_service(int argc, char **argv) {
    net_element_t *ele = get_element_by_tag(ELEMENT_RDF, "RDF");
    if (!ele) {
        log_warn("No net element");
        return;
    }

    print_as_info(ele, ele->as_map,  SERIAL_TAG | SAC_TAG | AS_IP_TAG | GS_IP_TAG | GS_PORT_TAG );
}
